home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8507 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem Negating an Unsigned Char
  5. Date: 4 Mar 1996 11:23:57 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hfg0dINNfh2@keats.ugrad.cs.ubc.ca>
  8. References: <Dnnros.Lq.0.-s@hkusuc.hku.hk> <4he27sINNdel@keats.ugrad.cs.ubc.ca> <DnqMyr.4pF@cwi.nl>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <DnqMyr.4pF@cwi.nl>, Dik T. Winter <dik@cwi.nl> wrote:
  12.  >In article <4he27sINNdel@keats.ugrad.cs.ubc.ca> c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  13.  >...
  14.  > > >unsigned char a=0x11;
  15.  > > >unsigned char b=0xEE;
  16.  >...
  17.  > > >    if( a == ~b ) {
  18.  >...
  19.  > > >The c remains unchange, while it changes to 1 if I cast the ~b to unsigned
  20.  > > >char as if( a == (unsigned char) ~b )
  21.  > > 
  22.  > > The cast forces the integer value of ~b into an unsigned char, stripping
  23.  > > high-order bits. The above will work only on machines with eight bit chars.
  24.  > > 
  25.  > > To make it portable, you must manually mask for the lower eight bits:
  26.  > > 
  27.  > >     if (a == (~b & 0xff))
  28.  >
  29.  >And how would this work on machines with other than eight bit chars?
  30.  >Casting to unsigned char will work regardless the number of bits in a char.
  31.  
  32. You are a fool. The poster obviously wants eight bit arithmetic, such that the
  33. complement of 0xEE is guaranteed to be 0x11. Simply casting to unsigned char
  34. will fail if you want portable eight bit arithmetic. On machines with eight bit
  35. unsigned chars, the complement of 0xEE will be 0x11, as he apparently expects.
  36. On machines with CHAR_BITS greater than eight, the complement of 0xEE is not
  37. 0x11 when cast to unsigned char.
  38.  
  39. Are you suggesting to this newsgroup that implementation-defined casts between
  40. incompatible arithmetic types are more portable than using well-defined
  41. bit operations?
  42. -- 
  43.  
  44.